Public Class frmManageCategory

  Private mObjclsManageCategory As New clsManageCategory
  Private mObjClsGenClas As New clsGenClas

  Private Sub frmManageCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    cboFEDBDataType.DataSource = mFnReadElements(TM_FE_DB_CATEGORY, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
    cboBEDBDataType.DataSource = mFnReadElements(TM_BE_DB_CATEGORY, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
    cboBEDataType.DataSource = mFnReadElements(TM_BE_CATEGORY, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)

    ResetControl()
    PopulateDataCategory()

  End Sub

  Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click
    ResetControl()
  End Sub

  Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
    Dim lStrFEDBDataCategory As String = String.Empty
    Dim lStrBEDBDataCategory As String = String.Empty
    Dim lStrBEDataType As String = String.Empty

    Try
      If ValidateInput() = False Then Exit Sub

      mFnReadValue(TM_FE_DB_CATEGORY, cboFEDBDataType.Text, String.Empty, lStrFEDBDataCategory, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
      mFnReadValue(TM_BE_DB_CATEGORY, cboBEDBDataType.Text, String.Empty, lStrBEDBDataCategory, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
      mFnReadValue(TM_BE_CATEGORY, cboBEDataType.Text, String.Empty, lStrBEDataType, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)

      If mObjclsManageCategory.CheckCategory(txtDataCategory.Text.Trim, _
                                             lStrFEDBDataCategory, _
                                             lStrBEDBDataCategory, _
                                             txtFECategory.Text.Trim, _
                                             lStrBEDataType) <> 0 Then
        MsgBox("This Category Already Exists", MsgBoxStyle.Information, gCONST_MSG_HEADER)
        Exit Sub
      End If

      mObjclsManageCategory.InsertCategory(txtCategoryDesc.Text.Trim, _
                                            numLength.Value, _
                                            numPrecision.Value, _
                                            txtDataCategory.Text.Trim, _
                                            lStrFEDBDataCategory, _
                                            lStrBEDBDataCategory, _
                                            txtFECategory.Text.Trim, _
                                            lStrBEDataType)

      With dgvCategory.Rows(dgvCategory.Rows.Add)
        .Cells("clmCategoryDesc").Value = txtCategoryDesc.Text.Trim
        .Cells("clmDataCategory").Value = txtDataCategory.Text.Trim
        .Cells("clmLength").Value = numLength.Value
        .Cells("clmPrecision").Value = numPrecision.Value
        .Cells("clmFEDBDataType").Value = lStrFEDBDataCategory
        .Cells("clmBEDBDataType").Value = lStrBEDBDataCategory
        .Cells("clmBEDataType").Value = lStrBEDataType
        .Cells("clmFEDataCategory").Value = txtFECategory.Text.Trim
      End With
      dgvCategory.ClearSelection()
      cmdViewCategory.Enabled = True
    Catch ex As Exception
      MsgBox("Error" & vbCrLf & ex.Message, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End Try

  End Sub

  Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModify.Click
    Dim lStrFEDBDataCategory As String = String.Empty
    Dim lStrBEDBDataCategory As String = String.Empty
    Dim lStrBEDataType As String = String.Empty

    Try
      If dgvCategory.SelectedRows.Count <> 1 Then
        MsgBox("Please Select One Category", MsgBoxStyle.Information, gCONST_MSG_HEADER)
        Exit Sub
      End If

      If ValidateInput() = False Then Exit Sub

      mFnReadValue(TM_FE_DB_CATEGORY, cboFEDBDataType.Text, String.Empty, lStrFEDBDataCategory, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
      mFnReadValue(TM_BE_DB_CATEGORY, cboBEDBDataType.Text, String.Empty, lStrBEDBDataCategory, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
      mFnReadValue(TM_BE_CATEGORY, cboBEDataType.Text, String.Empty, lStrBEDataType, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)

      mObjclsManageCategory.UpdateCategory(txtCategoryDesc.Text.Trim, _
                                          numLength.Value, _
                                          numPrecision.Value, _
                                          txtDataCategory.Text.Trim, _
                                          lStrFEDBDataCategory, _
                                          lStrBEDBDataCategory, _
                                          txtFECategory.Text.Trim, _
                                          lStrBEDataType)

      With dgvCategory.SelectedRows(0)
        .Cells("clmCategoryDesc").Value = txtCategoryDesc.Text.Trim
        .Cells("clmDataCategory").Value = txtDataCategory.Text.Trim
        .Cells("clmLength").Value = numLength.Value
        .Cells("clmPrecision").Value = numPrecision.Value
        .Cells("clmFEDBDataType").Value = lStrFEDBDataCategory
        .Cells("clmBEDBDataType").Value = lStrBEDBDataCategory
        .Cells("clmBEDataType").Value = lStrBEDataType
        .Cells("clmFEDataCategory").Value = txtFECategory.Text.Trim
      End With
      dgvCategory.ClearSelection()
      cmdViewCategory.Enabled = True
    Catch ex As Exception
      MsgBox("Error" & vbCrLf & ex.Message, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End Try

  End Sub

  Private Sub dgvCategory_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCategory.CellClick

    If e.RowIndex = -1 Then
      Exit Sub
    End If

    If e.ColumnIndex = 0 Then
      If MsgBox("Delete category " & dgvCategory.Rows(e.RowIndex).Cells("clmDataCategory").Value & "?", MsgBoxStyle.YesNo, gCONST_MSG_HEADER) = MsgBoxResult.Yes Then
        mObjclsManageCategory.DeleteCategory(dgvCategory.Rows(e.RowIndex).Cells("clmDataCategory").Value, _
                                             dgvCategory.Rows(e.RowIndex).Cells("clmFEDBDataType").Value, _
                                             dgvCategory.Rows(e.RowIndex).Cells("clmBEDBDataType").Value, _
                                             dgvCategory.Rows(e.RowIndex).Cells("clmFEDataCategory").Value, _
                                             dgvCategory.Rows(e.RowIndex).Cells("clmBEDataType").Value)
        dgvCategory.Rows.RemoveAt(e.RowIndex)
        dgvCategory.ClearSelection()
        cmdViewCategory.Enabled = True
      End If
    Else
      Call dgvCategory_SelectionChanged(sender, New System.EventArgs)
    End If
  End Sub

  Private Sub dgvCategory_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvCategory.SelectionChanged

    If dgvCategory.SelectedRows.Count <> 0 And dgvCategory.RowCount <> 0 Then
      If dgvCategory.CurrentCell.ColumnIndex <> 0 Then
        cmdViewCategory.Enabled = False
        txtDataCategory.Text = dgvCategory.SelectedRows.Item(0).Cells("clmDataCategory").Value
        txtFECategory.Text = dgvCategory.SelectedRows.Item(0).Cells("clmFEDataCategory").Value
        txtCategoryDesc.Text = dgvCategory.SelectedRows.Item(0).Cells("clmCategoryDesc").Value
        If IsDBNull(dgvCategory.SelectedRows.Item(0).Cells("clmLength").Value) Then
          numLength.Value = 0
        Else
          numLength.Value = dgvCategory.SelectedRows.Item(0).Cells("clmLength").Value
        End If
        If IsDBNull(dgvCategory.SelectedRows.Item(0).Cells("clmPrecision").Value) Then
          numPrecision.Value = 0
        Else
          numPrecision.Value = dgvCategory.SelectedRows.Item(0).Cells("clmPrecision").Value
        End If

        cboFEDBDataType.Text = mFnReadElementFromValue(TM_FE_DB_CATEGORY, dgvCategory.SelectedRows.Item(0).Cells("clmFEDBDataType").Value, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
        cboBEDBDataType.Text = mFnReadElementFromValue(TM_BE_DB_CATEGORY, dgvCategory.SelectedRows.Item(0).Cells("clmBEDBDataType").Value, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
        cboBEDataType.Text = mFnReadElementFromValue(TM_BE_CATEGORY, dgvCategory.SelectedRows.Item(0).Cells("clmBEDataType").Value, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
      End If
    End If

  End Sub

  Private Sub cmdViewCategory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewCategory.Click
    If digSelectCategory.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
    txtDataCategory.Text = digSelectCategory.mStrCategory
    txtFECategory.Text = digSelectCategory.mStrFECategory
  End Sub

  Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
    Dim lStrFileList As String = String.Empty
    Dim lStrFolderPath As String = String.Empty

    Dim lStrDateCategory As String = String.Empty
    Dim lStrBaseCategory As String = String.Empty
    Dim lStrCodeClientIndc As String = String.Empty

    If dgvCategory.SelectedRows.Count <> 1 Then
      MsgBox("Please Select One Category", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    Try
      If SelectFolder(lStrFolderPath) = False Then Exit Sub

      With dgvCategory.SelectedRows(0)
        lStrDateCategory = .Cells("clmFEDataCategory").Value.ToString.Trim
        If cboCodeClientIndc.Text = "FE DATA TYPE" Then
          lStrCodeClientIndc = CLIENT_INDC_FE & CLIENT_INDC_CATEGORY
          lStrBaseCategory = mFnReadAttribute(TM_FE_DB_CATEGORY, mFnReadElementFromValue(TM_FE_DB_CATEGORY, .Cells("clmFEDBDataType").Value.ToString.Trim, Application.StartupPath + "\" + CODE_TEMPLATE_FILE), TM_DATA_TYPE, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
        ElseIf cboCodeClientIndc.Text = "BE DATA TYPE" Then
          lStrCodeClientIndc = CLIENT_INDC_BE & CLIENT_INDC_CATEGORY
          lStrBaseCategory = mFnReadAttribute(TM_BE_DB_CATEGORY, mFnReadElementFromValue(TM_BE_DB_CATEGORY, .Cells("clmBEDBDataType").Value.ToString.Trim, Application.StartupPath + "\" + CODE_TEMPLATE_FILE), TM_DATA_TYPE, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
        End If
      End With

      lStrFileList = lStrFileList & GenerateCode(lStrDateCategory, lStrBaseCategory, lStrCodeClientIndc, lStrFolderPath) & vbCrLf & "  "

      MsgBox("Generate successfully - " & vbCrLf & "  " & lStrFileList, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Catch ex As Exception
      MsgBox("Error" & vbCrLf & ex.Message, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End Try

  End Sub

  Private Sub cmdGenerateAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerateAll.Click
    Dim lStrFileList As String = String.Empty
    Dim lStrFolderPath As String = String.Empty

    Dim lStrDateCategory As String = String.Empty
    Dim lStrBaseCategory As String = String.Empty
    Dim lStrCodeClientIndc As String = String.Empty

    Try
      If SelectFolder(lStrFolderPath) = False Then Exit Sub

      For Each lObjRow As DataGridViewRow In dgvCategory.Rows
        With lObjRow
          lStrDateCategory = .Cells("clmFEDataCategory").Value.ToString.Trim
          If cboCodeClientIndc.Text = "FE DATA TYPE" Then
            lStrCodeClientIndc = CLIENT_INDC_FE & CLIENT_INDC_CATEGORY
            lStrBaseCategory = mFnReadAttribute(TM_FE_DB_CATEGORY, mFnReadElementFromValue(TM_FE_DB_CATEGORY, .Cells("clmFEDBDataType").Value.ToString.Trim, Application.StartupPath + "\" + CODE_TEMPLATE_FILE), TM_DATA_TYPE, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
          ElseIf cboCodeClientIndc.Text = "BE DATA TYPE" Then
            lStrCodeClientIndc = CLIENT_INDC_BE & CLIENT_INDC_CATEGORY
            lStrBaseCategory = mFnReadAttribute(TM_BE_DB_CATEGORY, mFnReadElementFromValue(TM_BE_DB_CATEGORY, .Cells("clmBEDBDataType").Value.ToString.Trim, Application.StartupPath + "\" + CODE_TEMPLATE_FILE), TM_DATA_TYPE, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
          End If
          GenerateCode(lStrDateCategory, lStrBaseCategory, lStrCodeClientIndc, lStrFolderPath & "\" & lStrCodeClientIndc)
        End With
      Next

      MsgBox("Generate successfully in folder - " & vbCrLf & "  " & lStrFolderPath, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Catch ex As Exception
      MsgBox("Error" & vbCrLf & ex.Message, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End Try
  End Sub


  Private Sub ResetControl()
    cboFEDBDataType.SelectedIndex = 0
    cboBEDBDataType.SelectedIndex = 0
    cboCodeClientIndc.SelectedIndex = 0
    cboCodeClientIndc.Enabled = False
    cboBEDataType.SelectedIndex = 0
    txtDataCategory.Clear()
    txtFECategory.Clear()
    txtCategoryDesc.Clear()
    numLength.Value = 0
    numPrecision.Value = 0
    cmdViewCategory.Enabled = True
  End Sub

  Private Sub PopulateDataCategory()
    Dim lObjDataReader As OracleClient.OracleDataReader
    Dim lIntrowIndex As Integer

    lObjDataReader = mObjclsManageCategory.PopulateAllCategory()
    If IsNothing(lObjDataReader) = False Then
      dgvCategory.Rows.Clear()
      While lObjDataReader.Read
        lIntrowIndex = dgvCategory.Rows.Add()
        With dgvCategory.Rows(lIntrowIndex)
          .Cells("clmCategoryDesc").Value = lObjDataReader.Item("TDCM_DESC").ToString
          .Cells("clmDataCategory").Value = lObjDataReader.Item("TDCM_DATA_CTGRY").ToString
          .Cells("clmLength").Value = lObjDataReader.Item("TDCM_DATA_LENGTH")
          .Cells("clmPrecision").Value = lObjDataReader.Item("TDCM_DATA_PRECISION")
          .Cells("clmFEDBDataType").Value = lObjDataReader.Item("TDCM_FRNT_END_DATA_TYPE").ToString
          .Cells("clmBEDBDataType").Value = lObjDataReader.Item("TDCM_DATABASE_DATA_TYPE").ToString
          .Cells("clmBEDataType").Value = lObjDataReader.Item("TDCM_BACK_END_DATA_TYPE").ToString
          .Cells("clmFEDataCategory").Value = lObjDataReader.Item("TDCM_FRNT_END_DATA_CATEGORY").ToString
        End With
      End While
      dgvCategory.ClearSelection()
      cmdViewCategory.Enabled = True
    End If


  End Sub

  Private Function ValidateInput() As Boolean

    If Trim(txtDataCategory.Text) = "" Or Trim(txtFECategory.Text) = "" Then
      MsgBox("Please Enter Data Category", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Return False
    End If

    If Trim(txtCategoryDesc.Text) = "" Then
      MsgBox("Please Enter Category Description", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Return False
    End If

    If numLength.Value = 0 Then
      MsgBox("Please Enter Length of Category", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Return False
    End If

    Return True
  End Function

  Private Function GenerateCode(ByVal aStrDateCategory As String, _
                                ByVal aStrBaseCategory As String, _
                                ByVal aStrCodeClientIndc As String, _
                                ByVal aStrFolderPath As String) As String
    Dim lStrFileList As String = String.Empty
    Dim lStrModels As String = String.Empty
    Dim lStrFileName As String = String.Empty
    Dim lStrModelName As String = String.Empty
    Dim lStrSaveClientIndc As String = String.Empty
    Dim lArrModels() As String

    mFnReadValue(TM_TEMPLATE_MODEL, aStrCodeClientIndc, String.Empty, lStrModels, Application.StartupPath + "\" + CODE_TEMPLATE_FILE)
    lArrModels = lStrModels.Split(",")

    For Each lStrModel As String In lArrModels
      lStrModelName = lStrModel.Split("|")(0)
      lStrFileName = lStrModel.Split("|")(1)
      lStrFileName = lStrFileName.Replace("%" & TM_CATEGORY & "%", aStrDateCategory.Trim)
      lStrFileName = lStrFileName.Replace("%" & TM_BASE_CATEGORY & "%", StrConv(aStrBaseCategory, VbStrConv.ProperCase))
      lStrFileList = lStrFileList & mObjClsGenClas.mFnGnrtCode(String.Empty, String.Empty, String.Empty, aStrDateCategory, aStrBaseCategory, Nothing, aStrFolderPath & "\" & aStrBaseCategory, lStrFileName, lStrModelName, aStrCodeClientIndc) & vbCrLf & "  "
    Next

    Return lStrFileList
  End Function

End Class